package service;/*
@Date:2024/12/4
*/

import javebean.House;
import tool.MyhandleDbReflection;
import tool.Utility;

import java.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class HouseService {

   House[]houses=new House[10];

    public HouseService() {
            for (int i = 0; i < houses.length; i++) {
                houses[i] = new House();
                houses[i].setId(String.valueOf(i));
            }
        }
        //查看房屋列表
    public static  List<House> Findlist () {
        List<House> list = MyhandleDbReflection.findList("select * from house", House.class);
        System.out.println("---------------房屋列表---------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getId()+"\t\t"+list.get(i).getName()+"\t\t"+list.get(i).getTel()+"\t\t"+list.get(i).getAddress()+"\t\t"+list.get(i).getMoney()
                    +"\t\t"+list.get(i).getState());
        }
        System.out.println("---------------房屋列表完成---------------");
        return list;

    }

        //添加
        public Object add () throws SQLException {
            House house = new House();
            Scanner scanner = new Scanner(System.in);

            System.out.println("-----添加房屋-----");
            System.out.println("请输入id");
            house.setId(scanner.next());
            System.out.println("请输入新姓名");
            house.setName(scanner.next());
            System.out.println("请输入电话");
            house.setTel(scanner.next());
            System.out.println("请输入地址");
            house.setAddress(scanner.next());
            System.out.println("请输入租金");
            house.setMoney(scanner.next());
            System.out.println("请输入房屋是否被出租");
            house.setState(scanner.next());

            MyhandleDbReflection.update("insert into house values(?,?,?,?,?,?)",house.getId(), house.getName(),house.getTel(),house.getAddress(),house.getMoney(),house.getState());
            return "添加成功";

//            System.out.println("-----添加房屋-----");
//            System.out.println("房源id");
         //   int x = Utility.readInt();
//            //houses[x] = new javebean.House();
//            if (x < 0 || x >= houses.length) {
//                System.out.println("无效的房源ID");
//                return false;
//            }
//            if (houses[x].getState().equals("已出租")) {
//                System.out.println("该房源已出租");
//                return false;
//            }
//                System.out.println("姓名");
//                this.houses[x].setName(Utility.readKeyBoard(4, false));
//                System.out.println("电话");
//                houses[x].setTel(Utility.readString(11));
//                System.out.println("地址");
//                houses[x].setAddress(Utility.readKeyBoard(20, false));
//                System.out.println("月租");
//                houses[x].setMoney(Utility.readKeyBoard(20, false));
//                houses[x].setState("已出租");
//                return true;
    }

        //删除
        public Object del () {
            System.out.println("-----删除房屋-----");
            System.out.println("请输入删除房源id");
            Scanner scanner = new Scanner(System.in);
            MyhandleDbReflection.update("delete from house where id=?",scanner.next());
            return "删除成功";

//            int x = Utility.readInt();
//            System.out.println("请确认是否删除(Y/N)请小心选择:");
//            char c = Utility.readConfirmSelection();
//            if (c == 'Y') {
//                //houses[x] = null;
//                for (int i = 0; i < houses.length; i++) {
//                    final House house = new House();
//                   house.setId(String.valueOf(x));
//                    houses[x]=house;
//
//                }
//                System.out.println("删除成功");
//            } else {
//                System.out.println("删除失败");
//                return false;
//            }
//            return true;
        }

        //查找
        public Object[] findByld () throws Exception {
            System.out.println("-----查找房屋-----");
            System.out.println("请输入查找房源id");
            Scanner scanner = new Scanner(System.in);
//            int x = Integer.parseInt(scanner.next());
//          if (houses[scanner.nextInt()].getState().equals("未出租"))
//              return null;
            Object[] house = MyhandleDbReflection.queryArray("select * from house where id=?",  scanner.next());
            System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");

//                for (Object obj : house) {
//                    System.out.print(obj);
//                }
            for (int i = 0; i < house.length; i++) {
                System.out.print(house[i]+"    ");
            }

            System.out.println();



            System.out.println("---------------房屋列表完成---------------");
            return house;


//            int x = Utility.readInt();
//           if (houses[x].getState().equals("未出租"))
//               return null;
//           return houses[x].toString();

        }
    public static int Update(House hous){
        final int update = MyhandleDbReflection.update(
                "UPDATE house " +
                        "SET name= ?,tel=?, address = ?, money = ?, state = ? " +
                        "WHERE id = ?",
                //"update tb_house set  name= ?,tel=?, address = ?, money = ?, state = ?, where id = ?",
                hous.getName(),hous.getTel(),hous.getAddress(),hous.getMoney(),hous.getState(),hous.getId());
        return update;
    }
        //修改
        public boolean chang () {
            System.out.println("请输入修改房源id");
            int x = Utility.readInt();
            if (houses[x].getId().equals("null"))
                return false;

            System.out.println("请输入修改姓名");
            String z = Utility.readKeyBoard(4, false);
            houses[x].setName(z);
            System.out.println("请输入修改电话");
            houses[x].setTel(Utility.readString(11));
            System.out.println("请输入修改地址");
            houses[x].setAddress(Utility.readKeyBoard(20, false));
            System.out.println("请输入修改月租");
            houses[x].setMoney(Utility.readKeyBoard(20, false));
            houses[x].setState("已出租");
            int Update=HouseService.Update(houses[x]);
            System.out.println("修改成功");
            return true;
        }

    private class house {
    }
}
